home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Shout3Ddemo / S3D_2E1.exe / Shout3d_runtime / codebase / custom_nodes / WavySceneEffect.java < prev   
Text File  |  2000-09-21  |  3KB  |  108 lines

  1. /**    
  2.     Company:        Eyematic Interfaces
  3.     Project:        Shout3D 2.0 Sample Code
  4.     Class:            InvertSceneEffect
  5.     Date:            March 23, 2000
  6.     Description:    A simple example of a PostRenderEffect that makes the scene have an animated waviness run through it.
  7.     (C) Copyright Eyematic Interfaces, Inc. - 1997-2000 - All rights reserved
  8.  */
  9.  
  10. package custom_nodes;
  11. import shout3d.core.*;
  12. import shout3d.*;
  13. import java.awt.Graphics;
  14.  
  15. /**
  16.  * InvertSceneEffect
  17.  * 
  18.  * @author Dave Westwood
  19.  */
  20.  
  21. public class WavySceneEffect extends PostRenderEffect {
  22.  
  23.     // Amplitude of the wave in pixels 
  24.     final public IntField  waveAmplitude    = new IntField(this, "waveAmplitude", Field.ANY, 30);
  25.     // Length of the wave in pixels
  26.     final public IntField  waveLength    = new IntField(this, "waveLength", Field.ANY, 60);
  27.     // Velocity of animation of the wave, in pixels per second.  
  28.     final public IntField  crawlVelocity    = new IntField(this, "crawlVelocity", Field.ANY, 10);
  29.     
  30.     /**
  31.      * Constructs a default InvertSceneEffect node.
  32.      */
  33.     public WavySceneEffect(){}
  34.     
  35.     int[] horizOffset = null;
  36.     double startClockT = -1;
  37.     int oldAmplitude = -1;
  38.     int oldWavelength = -1;
  39.     int rowShift, toPixel, rowFirst, rowLast;
  40.     
  41.     public void filter(Graphics offScreenGraphics, int surface_pixel_bits[], float z_buffer[], int deviceWidth, int deviceHeight){
  42.         
  43.         if (surface_pixel_bits == null)
  44.             return;
  45.         
  46.         // Store these to avoid any nastiness happening from field values that change
  47.         // while we are filtering.
  48.         int curWaveAmplitude = waveAmplitude.getValue();
  49.         int curWaveLength    = waveLength.getValue();
  50.         int curCrawlVelocity = crawlVelocity.getValue();
  51.         
  52.         // Allocate an array of horizontal offsets for each of the rows within a waveLength's cycle
  53.         if (horizOffset == null || oldAmplitude != curWaveAmplitude ||
  54.             oldWavelength != curWaveLength){
  55.  
  56.             horizOffset = new int[curWaveLength];
  57.             for (int i = 0; i < curWaveLength; i++){
  58.                 horizOffset[i] = (int) (curWaveAmplitude * Math.sin( 2 * 3.14159 * ((float)i) / curWaveLength));
  59.             }
  60.             oldAmplitude = curWaveAmplitude;
  61.             oldWavelength =  curWaveLength;
  62.         }
  63.         
  64.         // How much time has passed since beginning?  Need this for crawl which is
  65.         // velocity based.
  66.         if (startClockT == -1){
  67.             startClockT = getViewer().getClock().getAbsoluteTime();
  68.         }
  69.         double deltaT = getViewer().getClock().getAbsoluteTime() - startClockT;
  70.         
  71.         toPixel = 0;
  72.         for (int row=0; row<deviceHeight; row++){
  73.             
  74.             rowShift = horizOffset[ ((int)(row + (float)curCrawlVelocity * deltaT)) % curWaveLength ];
  75.             
  76.             rowFirst = toPixel;
  77.             rowLast  = toPixel + deviceWidth - 1;
  78.             if (rowShift > 0){
  79.                 for (int col=0; col<deviceWidth; col++){
  80.                     if (col + rowShift < deviceWidth){
  81.                         surface_pixel_bits[toPixel] = surface_pixel_bits[toPixel+rowShift];
  82.                     }
  83.                     else {
  84.                         surface_pixel_bits[toPixel] = surface_pixel_bits[rowLast];
  85.                     }
  86.                     toPixel++;
  87.                 }
  88.             }
  89.             else if (rowShift < 0){ 
  90.                 toPixel += deviceWidth - 1;
  91.                 for (int col=deviceWidth-1; col >= 0; col--){
  92.                     if (col + rowShift >= 0){
  93.                         surface_pixel_bits[toPixel] = surface_pixel_bits[toPixel+rowShift];
  94.                     }
  95.                     else {
  96.                         surface_pixel_bits[toPixel] = surface_pixel_bits[rowFirst];
  97.                     }
  98.                     toPixel--;
  99.                 }
  100.                 toPixel += deviceWidth + 1;
  101.             }
  102.             else {
  103.                 // rowShift is 0, skip to next row.
  104.                 toPixel += deviceWidth;
  105.             }
  106.         }
  107.     }
  108. }